<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="lzy">
    <title>lzy-</title>
    <style>
        html,body{
            height: 100%;
        }
        body{
            margin: 0;
            background-color: black;
            overflow: hidden;
        }
        main{
            width: 800px;
            height: 400px;
            border-radius: 5px;
            box-shadow: 0px 0px 4px white;
            position:relative;
            margin:200px auto;
            box-sizing: border-box;
            padding:30px 40px 20px;
            background-image: url(效果图/img/keys.png); 
            background-size: 720px 350px;
            background-repeat: no-repeat;
            background-position: center 30px;
        }
        main div{
            width: 80px;
            height: 350px;
            display: inline-block;
            float: left;
        }
        span{
            display: inline-block;
            width: 40px;
            height: 201px;
            background-color: black;
            position: absolute;
            top: 30px;
            z-index: 2;
        }
        .b1{
            left: 100px;
        }
        .b2{
            left: 180px;
        }
        .b3{
            left: 340px;
        }
        .b4{
            left: 420px;
        }
        .b5{
            left: 500px;
        }
        .b6{
            right: 100px;
        }
        .b7{
            right: 20px;
        }
    </style>
</head>
<body>
    <main>
        <div class="t1"></div>
        <div class="t2"></div>
        <div class="t3"></div>
        <div class="t4"></div>
        <div class="t5"></div>
        <div class="t6"></div>
        <div class="t7"></div>
        <div class="t8"></div>
        <div class="t9"></div>


    <span class="b1"></span>
    <span class="b2"></span>
    <span class="b3"></span>
    <span class="b4"></span>
    <span class="b5"></span>
    <span class="b6"></span>
    <span class="b7"></span>
    </main>
    <audio src="">
    </audio>
    
</body>
</html>

<script>
    var div = document.querySelectorAll('div');
    var audio = document.querySelector('audio');
    // 鼠标
    for(var i=0;i<div.length;i++){
        div[i].onmousedown = function(){
            var name=this.className;          
            this.style.backgroundColor="#eee";
            src=name.slice(1);
            audio.src="notes/"+src+".oga";
            audio.play();
            console.log(audio.src);
        }
        div[i].onmouseup = function(){
            this.style.backgroundColor='transparent';
        }
    }

    // 键盘
    document.onkeydown=function(event){            
            //  alert(event.keyCode);
             var code=event.keyCode;
             if(code==49){
                 audio.src='notes/1.oga';
                 audio.play();
             }
             if(code==50){
                 audio.src='notes/2.oga';
                 audio.play();
             }
             if(code==51){
                 audio.src='notes/3.oga';
                 audio.play();
             }
             if(code==52){
                 audio.src='notes/4.oga';
                 audio.play();
             }
             if(code==53){
                 audio.src='notes/5.oga';
                 audio.play();
             }
             if(code==54){
                 audio.src='notes/6.oga';
                 audio.play();
             }
             if(code==55){
                 audio.src='notes/7.oga';
                 audio.play();
             }
             if(code==56){
                 audio.src='notes/8.oga';
                 audio.play();
             }
             if(code==57){
                 audio.src='notes/9.oga';
                 audio.play();
             }

         }; 
</script>